package com.keepstudy.config;

import com.keepstudy.constant.RabbitMQConstant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Shengkang.Mo
 * @create 2021-02-09 14:09
 */
@Configuration
public class RabbitMQConfig {

    //声明业务 Exchange
    @Bean("businessExchange")
    public FanoutExchange businessExchange() {
        return new FanoutExchange(RabbitMQConstant.BUSINESS_EXCHANGE_NAME);
    }

    //声明死信 Exchange
    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(RabbitMQConstant.DEAD_LETTER_EXCHANGE);
    }

    //声明业务队列 A
    @Bean("businessQueueA")
    public Queue businessQueueA() {
        Map<String, Object> args = new HashMap<>(RabbitMQConstant.DEFAULT_INITIAL_CAPACITY_SIZE);
        //x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        //x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", RabbitMQConstant.DEAD_LETTER_QUEUEA_ROUTING_KEY);
        return QueueBuilder.durable(RabbitMQConstant.BUSINESS_QUEUEA_NAME).withArguments(args).build();
    }

    //声明业务队列 B
    @Bean("businessQueueB")
    public Queue businessQueueB() {
        Map<String, Object> args = new HashMap<>(RabbitMQConstant.DEFAULT_INITIAL_CAPACITY_SIZE);
        //x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        //x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key", RabbitMQConstant.DEAD_LETTER_QUEUEB_ROUTING_KEY);
        return QueueBuilder.durable(RabbitMQConstant.BUSINESS_QUEUEB_NAME).withArguments(args).build();
    }

    //声明死信队列 A
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA() {
        return new Queue(RabbitMQConstant.DEAD_LETTER_QUEUEA_NAME);
    }

    //声明死信队列 B
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB() {
        return new Queue(RabbitMQConstant.DEAD_LETTER_QUEUEB_NAME);
    }

    // 声明业务队列A绑定关系
    @Bean
    public Binding businessBindingA(@Qualifier("businessQueueA") Queue queue,
                                    @Qualifier("businessExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

    // 声明业务队列B绑定关系
    @Bean
    public Binding businessBindingB(@Qualifier("businessQueueB") Queue queue,
                                    @Qualifier("businessExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }

    // 声明死信队列A绑定关系
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMQConstant.DEAD_LETTER_QUEUEA_ROUTING_KEY);
    }

    // 声明死信队列B绑定关系
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with(RabbitMQConstant.DEAD_LETTER_QUEUEB_ROUTING_KEY);
    }
}
